const data = {
  name: "zhangsan",
  age: 20,
};

// observer数据监听器，对整个页面的所有数据进行监听
const observer = (data, key, value) => {
  Object.defineProperty(data, key, {
    get() {
      return value;
    },
    set(val) {
      console.log(`${key}的值从${value}变成了${val}`);
      value = val;
      console.log("页面需要更新了");
    },
  });
};

Object.keys(data).forEach((item) => {
  observer(data, item, data[item]);
});

console.log(data.name); // "zhangsan"
data.name = "lisi";
console.log(data.name); // "lisi"

data.age = 30;
